package com.wjk.kylin.modules.system.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.dict.entity.DictType;
import com.wjk.kylin.modules.system.dict.service.IDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

/**
 * <p>
 *  字典类型表 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-07
 */
@Api("字典类型")
@RestController
@RequestMapping("/sys/dictType")
public class DictTypeController extends BaseController {

    @Autowired
    private IDictTypeService dictTypeService;

    @PreAuthorize("hasAuthority('system:dict:list')")
    @ApiOperation("列表")
    @Log(title = "字典类型-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(DictType dictType) {
        IPage<DictType> page = dictTypeService.findPage(new RequestData<DictType>(), dictType);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @ApiOperation("详情")
    @Log(title = "字典类型-详情",businessType = GET)
    @GetMapping("/{dictTypeId}")
    public ResponseData get(@PathVariable String dictTypeId) {
        DictType dictType = dictTypeService.getById(dictTypeId);
        return ResponseData.success().data("dictType", dictType);
    }

    @PreAuthorize("hasAuthority('system:dict:add')")
    @ApiOperation("新增")
    @Log(title = "字典类型-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody DictType dictType) {
        boolean flag = dictTypeService.save(dictType);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dict:edit')")
    @ApiOperation("编辑")
    @Log(title = "字典类型-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody DictType dictType) {
        boolean flag = dictTypeService.update(dictType);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dict:delete')")
    @ApiOperation("删除")
    @Log(title = "字典类型-删除",businessType = DELETE)
    @DeleteMapping("/{dictTypeIds}")
    public ResponseData delete(@PathVariable String[] dictTypeIds) {

        boolean flag = dictTypeService.deleteByIds(dictTypeIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }

    @PreAuthorize("hasAuthority('system:dict:delete')")
    @ApiOperation("清空缓存")
    @Log(title = "字典类型-清空缓存", businessType = CLEAN)
    @DeleteMapping("/clearCache")
    public ResponseData clearCache() {
        dictTypeService.clearCache();
        return ResponseData.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/select")
    public ResponseData select(){
        List<DictType> list = dictTypeService.list();
        return ResponseData.success().data("list",list);
    }
}
